<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Balance;

class BalanceController extends Controller
{
	


	public function records()
	{
		$user = request()->user();
		$condition = request('condition');
		

		if(!$condition){
			$query = Balance::query();

		}else{

			$query = Balance::query();
			if($condition=="withdraw"){

				$query = $query->withdraw();
			}elseif($condition == "pay"){

				$query = $query->pay();
			}else{
				$query = $query->where("remark",$condition);
			}

		}
		
		
		
		$query = $query->where('user_id', $user->id);
		$query = !empty(request('start_time')) ? $query->where('created_at', '>', request('start_time')) : $query;
		$query = !empty(request('end_time')) ? $query->where('created_at', '<', request('end_time')) : $query;

		$total = $query->sum('money');
		$cards = $query->orderByDesc('id')->paginate();
		
		

		
		return ['total' => $total, 'data' => $cards];
	}
}
